/**
 * 自定义函数
 * */


//列表
function getIndex(){
    var uinfo = getAdminInfo();
    if (!uinfo) { return }
    var formData = {
        role_id: uinfo.role_id,
        depart_id: uinfo.depart_id,
        uid: uinfo.uid,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/index',
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code < 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            tbody.html(getHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//列表字符串
function getHtml(d){
    var h = '', i;
    h += '';
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].item_name + '</td>';
        h += '<td>' + d[i].task_name + '</td>';
        h += '<td>' + d[i].wh_name + '</td>';
        h += '<td>';
        h += d[i].starthour + '<br>-<br>' + d[i].endhour;
        h += '<td>' + d[i].staff_name + '</td>';
        h += '<td>' + d[i].status_name + '</td>';
        h += '</td>';
        h += '<td class="td-manage">';
        h += '<a href="javascript:;" class="ml-5" onclick="lay_edit(\'编辑\',\'wh-edit.html\', ' + d[i].id + ');">';
        h += '编辑';
        h += '<i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a>';
        h += '&nbsp;&nbsp;';
        h += '<a href="javascript:;" class="ml-5" onclick="lay_del(' + d[i].id + ');" style="color: red;">';
        h += ' 删除';
        h += '<i class="Hui-iconfont"></i>';
        h += '</a>';
        h += '<input type="hidden" id="item_id' + d[i].id +'" value="' + d[i].item_id + '"/>';
        h += '<input type="hidden" id="task_id' + d[i].id +'" value="' + d[i].task_id + '"/>';
        h += '<input type="hidden" id="wh_id' + d[i].id +'" value="' + d[i].wh_id + '"/>';
        h += '<input type="hidden" id="starthour' + d[i].id +'" value="' + d[i].starthour + '"/>';
        h += '<input type="hidden" id="endhour' + d[i].id +'" value="' + d[i].endhour + '"/>';
        h += '<input type="hidden" id="delay' + d[i].id +'" value="' + d[i].delay + '"/>';
        h += '<input type="hidden" id="delay_name' + d[i].id +'" value="' + d[i].delay_name + '"/>';
        h += '</td>';
        h += '</tr>';
    }
    return h;
}


//编辑弹窗
function lay_edit(title,url,id,w,h){
    url += '?id=' + id;
    var tt = $('#item_id' + id).val();
    var task_id = $('#task_id' + id).val();
    var wh_id = $('#wh_id' + id).val();
    var starthour = $('#starthour' + id).val();
    var endhour = $('#endhour' + id).val();
    var delay = $('#delay' + id).val();
    var delay_name = $('#delay_name' + id).val();
    tt = tt ? escape(tt) : '';
    task_id = task_id ? task_id : '';
    wh_id = wh_id ? wh_id : '';
    starthour = starthour ? escape(starthour) : '';
    endhour = endhour ? escape(endhour) : '';
    delay = delay ? delay : '';
    delay_name = delay_name ? escape(delay_name) : '';
    url += '&item_id=' + tt;
    url += '&task_id=' + task_id;
    url += '&wh_id=' + wh_id;
    url += '&start=' + starthour;
    url += '&end=' + endhour;
    url += '&delay=' + delay;
    url += '&delay_name=' + delay_name;
    h = 500;
    layer_show(title,url,w,h);
}


//获取详情
function getOne(){
    if (!id) { return }
    $('#gongshi').html(delay_name);
    var formData = {
        id: id,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/option',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            var itemList = res.data.item;
            var taskList = res.data.task;
            var whList = res.data.wh;
            var hourList = res.data.hour;
            var h = '', i;
            h = '';
            if (item_id == 0) {
                h += '<option value="0" selected>-选择项目-</option>';
            } else {
                h += '<option value="0">-选择项目-</option>';
            }
            for (i = 0; i < itemList.length; i ++) {
                if (item_id == itemList[i].id) {
                    h += '<option value="' + itemList[i].id + '" selected>';
                } else {
                    h += '<option value="' + itemList[i].id + '">';
                }
                h += itemList[i].title;
                h += '</option>';
            }
            $('#item_id').html(h);
            h = '';
            if (task_id == 0) {
                h += '<option value="0" selected>-选择任务-</option>';
            } else {
                h += '<option value="0">-选择任务-</option>';
            }
            for (i = 0; i < taskList.length; i ++) {
                if (task_id == taskList[i].id) {
                    h += '<option value="' + taskList[i].id + '" selected>';
                } else {
                    h += '<option value="' + taskList[i].id + '">';
                }
                h += taskList[i].title;
                h += '</option>';
            }
            $('#task_id').html(h);
            h = '';
            if (wh_id == 0) {
                h += '<option value="0" selected>-选择工时-</option>';
            } else {
                h += '<option value="0">-选择工时-</option>';
            }
            for (i = 0; i < whList.length; i ++) {
                if (wh_id == whList[i].id) {
                    h += '<option value="' + whList[i].id + '" selected>';
                } else {
                    h += '<option value="' + whList[i].id + '">';
                }
                h += whList[i].title;
                h += '</option>';
            }
            $('#wh_id').html(h);
            h = '';
            if (start == '') {
                h += '<option value="" selected>-选择时间段-</option>';
            } else {
                h += '<option value="">-选择时间段-</option>';
            }
            for (i = 0; i < hourList.length; i ++) {
                //h += '<option value="' + i + '">';
                if (start == hourList[i]) {
                    h += '<option value="' + hourList[i] + '" selected>';
                } else {
                    h += '<option value="' + hourList[i] + '">';
                }
                h += hourList[i];
                h += '</option>';
            }
            $('#start').html(h);
            h = '';
            if (end == '') {
                h += '<option value="" selected>-选择时间段-</option>';
            } else {
                h += '<option value="">-选择时间段-</option>';
            }
            for (i = 0; i < hourList.length; i ++) {
                //h += '<option value="' + i + '">';
                if (end == hourList[i]) {
                    h += '<option value="' + hourList[i] + '" selected>';
                } else {
                    h += '<option value="' + hourList[i] + '">';
                }
                h += hourList[i];
                h += '</option>';
            }
            $('#end').html(h);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//计算工时
function jisuan(){
    var start = $('#start').val();
    var end = $('#end').val();
    //if (start > end) {
    //    return;
    //}
    //console.log(start,end);
    //var fen = (end - start) * 10;
    //var shi = Math.floor(fen / 60);
    //var fz = fen - shi * 60;
    //var hour = shi==0 ? fz + '分钟' : shi + '小时' + fz + '分钟';
    //console.log(fen,shi,fz,hour);
    //$('#gongshi').html(hour);
    if (start == '') { return }
    if (end == '') { return }
    if (start > end) { return }
    var formData = {
        id: id, start: start, end: end,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/jisuan',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            $('#gongshi').html(res.data.str);
            $('#delay')[0].value = res.data.fen;
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


////显示图片
//function showImg(e){
//    var h = '';
//    h += '<img src="' + e.img + '" style="width: 150px;"/>';
//    h += '<input type="hidden" id="img" value="' + e.img + '"/>';
//    $('#bigShow').html(h);
//}


//修改
function modify(){
    var uinfo = getAdminInfo();
    var formData = {
        role_id: uinfo.role_id,
        depart_id: uinfo.depart_id,
        staff_id: uinfo.uid,
        id: id,
        item_id: $('#item_id').val(),
        task_id: $('#task_id').val(),
        wh_id: $('#wh_id').val(),
        start: $('#start').val(),
        end: $('#end').val(),
        delay: $('#delay').val(),
    };
    //if (formData.title == '') {
    //    msgPop({msg: '标题必须的'}); return
    //}
    console.log(formData); //return
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/modify',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '操作成功'});
            setTimeout(function(){
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//删除
function lay_del(id) {
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg, function (index) {
        $.ajax({
            type: apiMethod,
            url: apiDomain + 'wh/del',
            data: {  id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg});
                    return
                }
                layer.msg('已删除!', {icon: 1, time: 1000});
                setTimeout(function () {
                    top.location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


//列表
function authIndex(){
    var uinfo = getAdminInfo();
    if (!uinfo) { return }
    var formData = {
        role_id: uinfo.role_id,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/auth-index',
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code < 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            tbody.html(authHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//列表字符串
function authHtml(d){
    var h = '', i;
    h += '';
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].item_name + '</td>';
        h += '<td>' + d[i].task_name + '</td>';
        h += '<td>' + d[i].wh_name + '</td>';
        h += '<td>';
        h += d[i].starthour + '<br>-<br>' + d[i].endhour;
        h += '<td>' + d[i].staff_name + '</td>';
        h += '<td>' + d[i].status_name + '</td>';
        h += '</td>';
        h += '<td class="td-manage">';
        h += '<a href="javascript:;" class="ml-5" onclick="lay_auth_ok(' + d[i].id + ');" style="color: green;">';
        h += '<b>通过</b>';
        h += '</a>';
        h += '<a href="javascript:;" class="ml-5" onclick="lay_auth_no(\'审核拒绝\',\'wh-auth-no.html\', ' + d[i].id + ');" style="color: red;">';
        h += '<b>拒绝</b>';
        h += '</a>';
        h += '</td>';
        h += '</tr>';
    }
    return h;
}


//审核通过
function lay_auth_ok(id) {
    var msg = '确认要 <b style="color: red; font-size: 20px;">通过</b> 吗？';
    layer.confirm(msg, function (index) {
        $.ajax({
            type: apiMethod,
            url: apiDomain + 'wh/auth-ok',
            data: {  id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg});
                    return
                }
                layer.msg('已通过!', {icon: 1, time: 1000});
                setTimeout(function () {
                    top.location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


//审核拒绝弹窗
function lay_auth_no(title,url,id,w,h){
    url += '?id=' + id;
    //var tt = $('#item_id' + id).val();
    //tt = tt ? escape(tt) : '';
    //url += '&item_id=' + tt;
    h = 350;
    layer_show(title,url,w,h);
}


//审核拒绝
function auth_no() {
    var remark = $('#remark').val();
    if (!remark) {
        msgPop({msg: '缘由必须的'}); return
    }
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/auth-no',
        data: {  id: id, remark: remark },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            layer.msg('已拒绝!', {icon: 1, time: 1000});
            setTimeout(function () {
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}